<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Default input function
if ( ! function_exists('input'))
{
	function input($id, $data=array())
	{
		$CI =& get_instance();
		$output = '';
		$extra = _parse_attr($data['attr']);

		$output .= $data['msg'] ? '<p class="msg">'.$data['msg'].'</p>' : ''; // Message

		// Validation types
		$extra .= $data['maximum'] ? ' data-maximum="'.$data['maximum'].'"' : ''; // Minimum length
		$extra .= $data['minimum'] ? ' required data-minimum="'.$data['minimum'].'"' : ''; // Minimum value or Required
		$extra .= $data['match'] ? ' data-match="'.$data['match'].'"' : ''; // Match another field (case sensitive)
		
		$extra .= !empty($data['regex']) ? ' data-regex="'.implode(',', $data['regex']).'"' : ''; // Ajax Match

		switch($data['type']) {

			case 'captcha':
				$output .= '<div id="captcha"></div>';
			break;

			// Assume just input box
			default:
				if($data['maximum'] > 0) {
					$extra .= ' maxlength="'.$data['maximum'].'"';
				}

				$data['type'] = $data['type'] ? $data['type'] : 'text';
				$output .= '<input type="'.$data['type'].'" id="'.$id.'" name="'.$id.'"'.$extra.' />';
			break;

		}

		return $output;
	}
}

// Open Form
if ( ! function_exists('open_form'))
{
	function open_form($id="form", $attr=array(), $ajax=true)
	{
		if(!$attr['action']) {
			$attr['action'] = current_url();
		}

		return '<form id="'.$id.'" name="'.$id.'" method="post"'.($ajax ? ' data-ajax' : '')._parse_attributes($attr)." >";
	}
}

// Close Form
if ( ! function_exists('close_form'))
{
	function close_form()
	{
		return '</form>';
	}
}

// Parse Attributes
if ( ! function_exists('_parse_attr'))
{
	function _parse_attr($attr)
	{
		if(empty($attr)) {
			return '';
		}

		$a = '';

		foreach ($attr as $key => $val)
		{
			$a .= ' '.$key.'="'.$val.'"';
		}

		return $a;
	}
}
